package com.csr.csrcheck.csrhtcontroller;

import com.csr.csrcheck.controller.BaseController;
import com.csr.csrcheck.controller.ex.CompanyException;
import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.News;
import com.csr.csrcheck.pojo.Product_type;
import com.csr.csrcheck.pojo.User;
import com.csr.csrcheck.service.*;
import com.csr.csrcheck.service.impl.NewsServiceImpl;
import com.csr.csrcheck.service.impl.RecallServiceImpl;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageSupport;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;

@RequestMapping({"csrht"})
@Controller
public class HTConntroller extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.csrhtcontroller.HTConntroller.class);

    @Resource
    private CompanyService companyService;

    @Resource
    private ProductService productService;

    @Resource
    private Abnormal_productsService abnormal_productsService;

    @Resource
    private NewsServiceImpl newsService;

    @Resource
    private ApprovalsService approvalsService;

    @Resource
    private Business_risksService business_risksService;

    @Resource
    private ClinicService clinicService;

    @Resource
    private ContendService contendService;

    @Resource
    private EvaluateService evaluateService;

    @Resource
    private Five_SupplierService fiveSupplierService;

    @Resource
    private IFlight_checkService iFlight_checkService;

    @Resource
    private Product_typeService productTypeService;

    @Resource
    private Important_notificationService important_notificationService;

    @Resource
    private LawsuitService lawsuitService;

    @Resource
    private RecallServiceImpl recallService;

    @Resource
    private ShareholderService shareholderService;

    @Resource
    private Special_bulletinService special_bulletinService;

    @Resource
    private Stock_alterationService stock_alterationService;

    @Resource
    private IUserService userService;

    @RequestMapping({"companyjsp"})
    public String findCompanyPage(@RequestParam(value = "company_name", required = false) String company_name, @RequestParam(value = "pageIndex", required = false) String pageIndex, Model model) {
        log.info("findCompanyPage----------------------->company_name:" + company_name);
        log.info("findCompanyPage----------------------->pageIndex:" + pageIndex);
        List<Company> list = null;
        int pageSize = 10;
        Integer currentPageNo = Integer.valueOf(1);
        if (pageIndex != null)
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        int totalCount = 0;
        try {
            totalCount = this.companyService.getCompanyCount(company_name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo.intValue());
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        if (currentPageNo.intValue() < 1) {
            currentPageNo = Integer.valueOf(1);
        } else if (currentPageNo.intValue() > totalPageCount) {
            currentPageNo = Integer.valueOf(totalPageCount);
        }
        try {
            list = this.companyService.getCommpanylistpage(company_name, currentPageNo, Integer.valueOf(pageSize));
            System.out.println(list.size());
        } catch (Exception e) {
            throw new CompanyException(e);
        }
        model.addAttribute("list", list);
        model.addAttribute("page", pages);
        model.addAttribute("company_name", company_name);
        return "company";
    }

    @RequestMapping({"product"})
    public String product(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Model model, @RequestParam(value = "name", required = false) String name) {
        log.info("product----------------------->pageNum:" + pageNum);
        log.info("product----------------------->pageSize:" + pageSize);
        log.info("product----------------------->name:" + name);
        PageResult pageResult = this.productService.listpage(pageNum, pageSize, name);
        log.info("product----------------------->totalSize:" + pageResult.getTotalSize());
        if (pageResult == null)
            throw new CompanyException("");
                    model.addAttribute("page", pageResult);
        model.addAttribute("name", name);
        return "Product";
    }

    @RequestMapping({"abnormal"})
    public String abnormal(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, @RequestParam(value = "name", required = false) String name, Model model) {
        log.info("abnormal------------------------->pageNum:" + pageNum);
        log.info("abnormal------------------------->pageSize:" + pageSize);
        log.info("abnormal------------------------->name:" + name);
        PageResult pageResult = this.abnormal_productsService.abnormallsitpage(pageNum, pageSize, name);
        log.info("abnormal------------------------->totalSize" + pageResult.getTotalSize());
        if (pageResult == null)
            throw new CompanyException("");
                    model.addAttribute("page", pageResult);
        model.addAttribute("name", name);
        return "abnormal";
    }

    @RequestMapping({"addnews"})
    public String add(@ModelAttribute("news") News news) {
        return "addnews";
    }

    @RequestMapping(value = {"add"}, method = {RequestMethod.POST})
    public String addNews(News news, @RequestParam("multipartFile") MultipartFile multipartFile, HttpServletRequest request, String newsdate, HttpSession session) {
        String imgurl = null;
        if (!multipartFile.isEmpty()) {
            String fileName = multipartFile.getOriginalFilename();
            String suffixName = FilenameUtils.getExtension(fileName);
            String filePath = "D://temp-rainy//";
            if (suffixName.equalsIgnoreCase("jpg") || suffixName.equalsIgnoreCase("jpeg") || suffixName
                    .equalsIgnoreCase("png") || suffixName.equalsIgnoreCase("pneg")) {
                fileName = System.currentTimeMillis() + "_" + (new Random()).nextInt(1000) + ".jpg";
                File dest = new File(filePath + fileName);
                if (!dest.getParentFile().exists())
                    dest.getParentFile().mkdirs();
                try {
                    multipartFile.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                    request.setAttribute("fileUploadError", " * ");
                    return "addnews";
                }
                imgurl = "/temp-rainy/" + fileName;
            } else {
                request.setAttribute("fileUploadError", " * ");
                return "addnews";
            }
        }
        news.setImg_url(imgurl);
        try {
            news.setDate((new SimpleDateFormat("yyyy-mm-dd")).parse(newsdate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        this.newsService.addNews(news);
        return "redirect:/csrht/newspage";
    }

    @RequestMapping({"newspage"})
    public String listpage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Model model) {
        PageResult pageResult = this.newsService.getNewspage(pageNum, pageSize);
        if (pageResult == null)
            throw new CompanyException("");
                    log.info("listpage---------------------------->pageNum" + pageNum);
        log.info("listpage---------------------------->pageSize" + pageSize);
        model.addAttribute("page", pageResult);
        return "news";
    }

    @RequestMapping({"approvals"})
    public String approvals(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Model model, String name) {
        PageResult pageResult = this.approvalsService.getApprovalslistpage(pageNum, pageSize, name);
        log.info("approvals--------------------------->pageNum:" + pageNum);
        log.info("approvals--------------------------->pageSize:" + pageSize);
        log.info("approvals--------------------------->name:" + name);
        model.addAttribute("page", pageResult);
        model.addAttribute("name", name);
        return "approvals";
    }

    @RequestMapping({"bussiness"})
    public String bussiness(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String company_name, Model model) {
        PageResult pageResult = this.business_risksService.getListpage(pageNum, pageSize, company_name);
        if (pageResult == null)
            throw new CompanyException("");
                    model.addAttribute("page", pageResult);
        model.addAttribute("name", company_name);
        log.info("bussiness---------------------------->pageNum:" + pageNum);
        log.info("bussiness---------------------------->pageSize:" + pageSize);
        log.info("bussiness---------------------------->company_name:" + company_name);
        return "bussiness";
    }

    @RequestMapping({"clinic"})
    public String clinic(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String product_name, Model model) {
        PageResult pageResult = this.clinicService.getListpage(pageNum, pageSize, product_name);
        log.info("clinic---------------------------->pageNum:" + pageNum);
        log.info("clinic---------------------------->pageSize:" + pageSize);
        log.info("clinic---------------------------->product_name:" + product_name);
        model.addAttribute("page", pageResult);
        model.addAttribute("name", product_name);
        return "clinic";
    }

    @RequestMapping({"contend"})
    public String contend(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String company_name, Model model) {
        PageResult pageResult = this.contendService.getListpage(pageNum, pageSize, company_name);
        log.info("contend---------------------------------->pageNum:" + pageNum);
        log.info("contend---------------------------------->pageSize:" + pageSize);
        log.info("contend---------------------------------->company_name:" + company_name);
        model.addAttribute("page", pageResult);
        model.addAttribute("name", company_name);
        return "contend";
    }

    @RequestMapping({"evaluate"})
    public String evaluate(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String company_name, String product_name, Model model) {
        PageResult pageResult = this.evaluateService.getlistpage(pageNum, pageSize, company_name, product_name);
        log.info("evaluate--------------------------->pageNum:" + pageNum);
        log.info("evaluate--------------------------->pageSize:" + pageSize);
        log.info("evaluate--------------------------->company_name:" + company_name);
        log.info("evaluate--------------------------->product_name:" + product_name);
        model.addAttribute("cname", company_name);
        model.addAttribute("pname", product_name);
        model.addAttribute("page", pageResult);
        return "evaluate";
    }

    @RequestMapping({"fiveSuppler"})
    public String fiveSuppler(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String supplier_name, Model model) {
        PageResult pageResult = null;
        try {
            pageResult = this.fiveSupplierService.getListFive(pageNum, pageSize, supplier_name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("fiveSuppler------------------>pageResult" + pageResult);
        log.info("fiveSuppler------------------>pageNum" + pageNum);
        log.info("fiveSuppler------------------>pageSize" + pageSize);
        log.info("fiveSuppler------------------>supplier_name" + supplier_name);
        model.addAttribute("name", supplier_name);
        model.addAttribute("page", pageResult);
        return "fiveSuppler";
    }

    @RequestMapping({"flightcheck"})
    public String flightcheck(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String company_name, String product_name, String product_t_name, String check_no, String publication, Model model) {
        PageResult pageResult = this.iFlight_checkService.getListpage(pageNum, pageSize, company_name, product_name, product_t_name, check_no, publication);
        if (pageResult == null)
            throw new CompanyException("");
                    List<Product_type> list = this.productTypeService.getlist();
        if (list == null)
            throw new CompanyException("");
                    log.info("flightcheck--------------------------->pageNum:" + pageNum);
        log.info("flightcheck--------------------------->pageSize:" + pageSize);
        log.info("flightcheck--------------------------->company_name:" + company_name);
        log.info("flightcheck--------------------------->product_name:" + product_name);
        log.info("flightcheck--------------------------->product_t_name:" + product_t_name);
        log.info("flightcheck--------------------------->check_no:" + check_no);
        log.info("flightcheck--------------------------->publication:" + publication);
        model.addAttribute("page", pageResult);
        model.addAttribute("company_name", company_name);
        model.addAttribute("product_name", product_name);
        model.addAttribute("product_t_name", product_t_name);
        model.addAttribute("check_no", check_no);
        model.addAttribute("publication", publication);
        model.addAttribute("list", list);
        return "flightcheck";
    }

    @RequestMapping({"important"})
    public String important(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String product_name, String product_t_name, Model model) {
        PageResult pageResult = this.important_notificationService.getimportant_page(pageNum, pageSize, product_name, product_t_name);
        if (pageResult == null)
            throw new CompanyException("");
                    List<Product_type> list = this.productTypeService.getlist();
        if (list == null)
            throw new CompanyException("");
                    log.info("important----------------------------->pageNum:" + pageNum);
        log.info("important----------------------------->pageSize:" + pageSize);
        log.info("important----------------------------->product_name:" + product_name);
        log.info("important----------------------------->product_t_name:" + product_t_name);
        model.addAttribute("page", pageResult);
        model.addAttribute("product_name", product_name);
        model.addAttribute("product_t_name", product_t_name);
        model.addAttribute("list", list);
        return "important";
    }

    @RequestMapping({"lawsuit"})
    public String lawsuit(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String company_name, String casetype, String doctype, String defendants, String submittime, Model model) {
        PageResult pageResult = this.lawsuitService.getlistpage(pageNum, pageSize, company_name, casetype, doctype, defendants, submittime);
        if (pageResult == null)
            throw new CompanyException("");
                    log.info("lawsuit-------------------------------------->pageNum:" + pageNum);
        log.info("lawsuit-------------------------------------->pageSize:" + pageSize);
        log.info("lawsuit-------------------------------------->company_name:" + company_name);
        log.info("lawsuit-------------------------------------->casetype:" + casetype);
        log.info("lawsuit-------------------------------------->doctype:" + doctype);
        log.info("lawsuit-------------------------------------->defendants:" + defendants);
        log.info("lawsuit-------------------------------------->submittime:" + submittime);
        model.addAttribute("page", pageResult);
        model.addAttribute("company_name", company_name);
        model.addAttribute("casetype", casetype);
        model.addAttribute("doctype", doctype);
        model.addAttribute("defendants", defendants);
        model.addAttribute("submittime", submittime);
        return "lawsuit";
    }

    @RequestMapping({"recall"})
    public String productrecall(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String company_name, String product_name, String product_t_name, Model model) {
        PageResult pageResult = this.recallService.getlistpage(pageNum, pageSize, company_name, product_name, product_t_name);
        if (pageResult == null)
            throw new CompanyException("");
                    List<Product_type> list = this.productTypeService.getlist();
        if (list == null)
            throw new CompanyException("");
                    log.info("productrecall----------------------------------------->pageNum:" + pageNum);
        log.info("productrecall----------------------------------------->pageSize:" + pageSize);
        log.info("productrecall----------------------------------------->company_name:" + company_name);
        log.info("productrecall----------------------------------------->product_name:" + product_name);
        log.info("productrecall----------------------------------------->product_t_name:" + product_t_name);
        model.addAttribute("page", pageResult);
        model.addAttribute("company_name", company_name);
        model.addAttribute("product_name", product_name);
        model.addAttribute("product_t_name", product_t_name);
        model.addAttribute("list", list);
        return "recall";
    }

    @RequestMapping({"shareholder"})
    public String shareholder(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String shareholder_name, String share_type, String company_name, Model model) {
        PageResult pageResult = this.shareholderService.getListPage(pageNum, pageSize, shareholder_name, share_type, company_name);
        if (pageResult == null)
            throw new CompanyException("");
                    model.addAttribute("page", pageResult);
        model.addAttribute("shareholder_name", shareholder_name);
        model.addAttribute("share_type", share_type);
        model.addAttribute("company_name", company_name);
        return "shareholder";
    }

    @RequestMapping({"special"})
    public String special(String release_time, String company_name, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Model model) {
        PageResult pageResult = this.special_bulletinService.getlistpage(release_time, company_name, pageNum, pageSize);
        if (pageResult == null)
            throw new CompanyException("");
                    model.addAttribute("page", pageResult);
        model.addAttribute("company_name", company_name);
        model.addAttribute("release_time", release_time);
        return "special";
    }

    @RequestMapping({"stock"})
    public String stock(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, String shareholder_name, String type, String change_time, Model model) {
        PageResult pageResult = null;
        try {
            pageResult = this.stock_alterationService.getlistPage(pageNum, pageSize, shareholder_name, type, change_time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("page", pageResult);
        model.addAttribute("type", type);
        model.addAttribute("change_time", change_time);
        model.addAttribute("shareholder_name", shareholder_name);
        log.info("stock------------------------->type:" + type);
        return "stock";
    }

    @GetMapping("personal")
    public String personzl(Model model, HttpSession session) {
        int id = getUidFromSession(session).intValue();
        User user = this.userService.finduser(id);
        model.addAttribute("user", user);
        return "personal";
    }
}
